package com.gcloud.googledrive;

import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
import com.google.api.client.http.FileContent;
import com.google.api.services.drive.Drive;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.google.api.services.drive.model.File;
import com.utils.GCloudUtil;

import java.io.IOException;

public class UploadGoogleFile extends AsyncTask<Void, Long, Boolean> {

	private Drive service;
	private Uri fileUri;
	private Context context;
	private static String TAG = UploadGoogleFile.class.getSimpleName();
	private final ProgressDialog dialog;
	private long sizeFile;
	private java.io.File regularFile;

	public UploadGoogleFile(Drive service, Uri fileUri, Context context) {
		this.service = service;
		this.fileUri = fileUri;
		this.context = context;
		dialog = new ProgressDialog(context);
		// if (context != null) {
		// dialog.setMax(100);
		// dialog.setMessage("Uploading " + fileUri.getPath());
		// dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		// dialog.setProgress(0);
		// dialog.show();
		// }
	}

	public UploadGoogleFile(Drive service, Context context,
			java.io.File regularFile) {
		this.service = service;		
		this.context = context;
		this.regularFile = regularFile;
		dialog = new ProgressDialog(context);
		// if (context != null) {
		// dialog.setMax(100);
		// dialog.setMessage("Uploading " + fileUri.getPath());
		// dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		// dialog.setProgress(0);
		// dialog.show();
		// }
	}

	@Override
	protected Boolean doInBackground(Void... arg0) {
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {

					java.io.File fileContent = regularFile == null ? new java.io.File(
							fileUri.getPath()) : regularFile;
					// new java.io.File(fileUri.getPath());
					String mimeType = "image/jpeg";
					// GCloudUtil.getMimeTypeFromURI(fileUri.getPath());
					FileContent mediaContent = new FileContent(mimeType,
							fileContent);
					File body = new File();
					body.setTitle(fileContent.getName());
					body.setMimeType(mimeType);
					sizeFile = fileContent.length();
					File file = service.files().insert(body, mediaContent)
							.execute();
					if (file != null) {
						//showToast("File uploaded: " + file.getTitle());
						Log.i(TAG, "File uploaded");
					}
				} catch (UserRecoverableAuthIOException e) {
					Log.i(TAG, "account user not authorized: " + e);
					// startActivityForResult(e.getIntent(),
					// REQUEST_AUTHORIZATION);
					return;
				} catch (IOException e) {
					Log.i(TAG, "IOException: " + e);
					return;
				}
			}
		});
		t.start();
		return true;
	}

	@Override
	protected void onProgressUpdate(Long... progress) {
		if (context != null) {
			int percent = (int) (100.0 * (double) progress[0] / sizeFile + 0.5);
			dialog.setProgress(percent);
		} else {
			super.onProgressUpdate(progress);
		}
	}

	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);
		String message = "File upload sucess";
		if (result) {
			showToast(message);
		} else {
			message = "File not uploaded";
			showToast(message);
		}
	}

	private void showToast(String message) {
		if (context != null) {
			Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
			toast.show();
		}
	}
}
